<template>
	<zui-box>

		<!-- 头部导航 -->
		<zui-navbar title='选择地址'></zui-navbar>

		<mescroll-body :height="mescroll_height +'px'" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption"
			:up="upOption">
			<u-gap height="30rpx"></u-gap>

			<view class="list">
				
				<view class="item" v-for="(item,index) in list" :key="index" @click="opt(item.id)" >
					<!-- //平面盒子 -->
					<zui-flat bgColor='#fff'>
						<!-- //地址列表 -->
						<zui-address 
						:name='item.name' :phone='item.phone' 
						:address="item.pro_name+'-'+item.city_name+'-'+item.area_name+'-'+item.address" 
						:icon='false'
						:is_opt='true'
						>
						<view class="opt_btn">
							选择
						</view>
						</zui-address>
					</zui-flat>
					<u-gap height="30rpx"></u-gap>
				</view>
			</view>

		</mescroll-body>

		<!-- //固定到底部 -->
		<zui-foot>
			<!-- //防抖按钮 -->
			<zui-button bgColor='#AA334F' @fun='save'>添加地址</zui-button>
		</zui-foot>



	</zui-box>
</template>

<script>
	import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js';
	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				list: [],
				// 下拉刷新的配置(可选, 绝大部分情况无需配置)
				downOption: {
					auto: false //是否在初始化后,自动执行downCallback; 默认true
				},
				upOption: {
					auto: false // 是否在初始化完毕之后自动执行上拉加载的回调; 默认true
				},

			};
		},
		onShow() {
			// this.getlist();
			this.$nextTick(()=>{
				this.mescroll.resetUpScroll();
			})
		},

		created() {
			let that = this;
			   uni.getSystemInfo({ 
				success(res) {
				console.log('一',res.windowHeight); //可使用窗口高度
				that.mescroll_height = res.windowHeight - that.$u.getPx(uni.$u.props.navbar.height) - that.$u.sys().statusBarHeight -80
				
				console.log('二',that.mescroll_height)
				}
			})
		},
		methods: {
			save(){
				this.xz_go('address_add');
			},
			//下拉刷新的回调, 重置列表为第一页 (此处可删,mixins已默认)
			downCallback() {
				setTimeout(() => {
					this.mescroll.resetUpScroll();
				}, 1000);
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				uni.$u.http
					.get('app/address/list', {
						params: {
							page: page.num,
						},
						custom: {
							auth: true,
							toast: false,
							catch: false,
						},
					})
					.then(res => {
						console.log('返回的数据', res);
						this.mescroll.endSuccess(res.length);
						//设置列表数据
						if (page.num == 1) this.list = []; //如果是第一页需手动制空列表
						this.list = this.list.concat(res); //追加新数据

					})
					.catch(res => {
						this.mescroll.endErr();
					});
			},
			//设置默认地址
			opt(id) {
				
				uni.$u.http
					.get('/app/address/default', {
						params: {
							is_default:1,
							id:id
						},
						custom: {
							auth: true,
							toast: false,
							catch: false,
						},
					})
					.then(res => {
						this.say('选择成功');
						this.xz_back();
						
					})
					.catch(res => {
						this.mescroll.endErr();
					});
				
			}
		}
	}
</script>

<style lang='scss' scoped>
	
	.opt_btn{
		color: #fff;
		background: #AA334F;
		border-radius: 30rpx;
		padding: 10rpx 30rpx;
	}

/* //空数据 */
	/deep/.mescroll-empty {
		box-sizing: border-box;
		width: 100%;
		padding: 400rpx 50rpx;
		text-align: center;
	}

</style>
